package com.homer.commons.sscpprotocol.server.impl;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;

import com.homer.commons.sscpprotocol.server.base.AbstractMessageEncoder;
import com.homer.commons.sscpprotocol.server.servlet.SscpServletRequest;
import com.homer.commons.sscpprotocol.server.servlet.SscpServletResponse;

public class ResponseEncoder extends AbstractMessageEncoder<SscpResponseImpl> {
	public ResponseEncoder(int version) {
		super(version);
	}

	protected void encodeMessage(IoSession session, SscpResponseImpl message, IoBuffer out) {
		if (message instanceof SscpServletResponse) {
			SscpResponseImpl sscpResponseImpl = (SscpResponseImpl) message;
			byte[] contentBytes = sscpResponseImpl.getContentBytes();
			out.put(sscpResponseImpl.getSync_head());
			out.put(sscpResponseImpl.getMainDeviceID());
			out.put(sscpResponseImpl.getSlaveDevideID());
			out.put(sscpResponseImpl.getKey());
			
			out.put(sscpResponseImpl.getCommandLength());
			if (sscpResponseImpl.getCommandLength() > 0) {
				out.put(contentBytes);
			}
			out.put(sscpResponseImpl.getSync_end());
		}

		if (message instanceof SscpServletRequest) {
			SscpServletRequest sscpServletRequest = (SscpServletRequest) message;
			byte[] contentBytes = sscpServletRequest.getContentBytes();
			out.put(sscpServletRequest.getSync_head());
			out.put(sscpServletRequest.getMainDeviceID());
			out.put(sscpServletRequest.getSlaveDevideID());
			out.put(sscpServletRequest.getKey());
			
			out.put(sscpServletRequest.getCommandLength());
			if (sscpServletRequest.getCommandLength() > 0) {
				out.put(contentBytes);
			}
			out.put(sscpServletRequest.getSync_end());
		}

	}

	public void dispose() throws Exception {
	}
}
